home *** CD-ROM | disk | FTP | other *** search
- Path: ocbbs.gen.nz!not-for-mail
- From: steve@hn.ocbbs.gen.nz (Steve Detoni)
- Newsgroups: comp.lang.c++
- Subject: Re: Copy commands - copy Binary files.
- Date: 10 Jan 1996 00:21:09 +1300
- Message-ID: <4ctj35$8ia@hn.ocbbs.gen.nz>
- References: <peterf.54.000F9B13@gears.efn.org>
- NNTP-Posting-Host: hn.hn.planet.gen.nz
- X-Newsreader: TIN [version 1.2 PL2]
-
- Peter F. (peterf@gears.efn.org) wrote:
- : Dear Friends,
- Hi there,
-
- : I have been working on a c++ program, where the goal is to imitate the dos
- : copy command. I have been writing this program, and it works eccept for one
- : small thing. When I read a file, say 29,300 bytes and then want to output
- : that file to a new directory, the files always come out 1 byte larger (ex.
- : 29,301). I dont know where the extra byte is coming from? Here is the code
- : and maybe you could find the problem....
- Hmm, yeah, sounds like DOS is putting a EOF char (cntl Z) after your
- close your file... Simple thing to fix!
-
- // Simple and fast copy program!
- #include <stdio.h>
- #include <fcntl.h>
-
- const int BUFFSIZE = 4096; // 4k!
- char Buffer[BUFFSIZE];
-
- int main (int argc, char** argv)
- {
- int inFile, outFile;
- int readSize;
-
- // parameters from command line
- switch (argc)
- {
- case 2:
- inFile = open (argv[1], O_RDONLY | O_BINARY);
- outFile = open (argv[2], O_CREAT);
- break;
-
- default:
- printf ("Usage: %s <inputfile> <outputfile>", argv[0]);
- return -1;
-
- }
- if (inFile < 0)
- {
- printf ("Error opening %s", argv[1]);
- close (outFile);
- return -2;
- }
- if (outFile < 0)
- {
- printf ("Error opening %s", argv[2]);
- close (inFile);
- return -3;
- }
- // Copy process !
- while ((readSize = read (inFile, Buffer, BUFFSIZE) > 0)
- {
- write (outFile, Buffer, readSize);
- }
- close (inFile);
- close (outFile);
- return 0;
- }
-
- // Program untested, but should work ... <coded from memory>
-
- // Should compile
- Steve.
-